/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.countwords.christophe;

import com.google.common.base.Splitter;
import fr.coding.kata.nantes.countwords.commons.KataCountWordsInterface;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

public class KataCountWordsJDK7 implements KataCountWordsInterface {
    private static final String EOL = System.getProperty("line.separator");
    private static final Splitter SPLITTER = Splitter.on(EOL);
    protected List<String> lines;
    protected String word;

    @Override
    public KataCountWordsInterface withText(String anText) {
        Iterable<String> strings = SPLITTER.split(anText);
        lines = new ArrayList<String>();
        for (String line : strings) {
            lines.add(line);
        }
        return this;
    }

    @Override
    public KataCountWordsInterface withWord(String anWord) {
        word = anWord;
        return this;
    }

    @Override
    public Integer call() throws Exception {
        ForkJoinPool pool = new ForkJoinPool();
        CountWordLineTask task = new CountWordLineTask(lines);
        pool.invoke(task);
        return task.result;
    }


    private class CountWordLineTask extends RecursiveAction {
        private final List<String> lines;
        private int result;


        CountWordLineTask(List<String> anLines) {
            lines = anLines;
        }

        protected void compute() {
            int nbLine = lines.size();
            if (nbLine == 0) {
                result = 0;
            } else if (nbLine == 1) {
                result = countWordInLine(lines.get(0));
            } else {

                int nbSubLines = nbLine / 2;

                List<String> lines1 = new ArrayList<String>();
                for (int i = nbSubLines; i > 0; i--) {
                    lines1.add(lines.get(i));
                }

                List<String> lines2 = new ArrayList<String>();
                for (int i = nbSubLines; i < nbLine; i++) {
                    lines1.add(lines.get(i));
                }

                CountWordLineTask task1 = new CountWordLineTask(lines1);
                CountWordLineTask task2 = new CountWordLineTask(lines2);

                invokeAll(task1, task2);
                result = task1.result + task2.result;
            }
        }

        public Integer countWordInLine(String line) {
//            int count = -1;
//            int index = -1;
//            int wordSize = word.length();
//            do {
//                index = line.indexOf(word, index + wordSize);
//                count++;
//            } while (index != -1);
//            return count;

            return StringUtils.countMatches(line, word);
        }
    }
}
